package com.heima.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.feign.ApBehaviorEntryFeign;
import com.heima.common.dtos.Result;
import com.heima.common.exception.AppHttpCodeEnum;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.behavior.pojos.ApBehaviorEntry;

import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.pojos.ApUserSearch;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.mapper.ApUserSearchMapper;
import com.heima.search.service.ApUserSearchService;
import com.heima.utils.common.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {
    @Autowired
    private ApBehaviorEntryFeign apBehaviorEntryFeign;
    @Autowired
    private ApUserSearchMapper apUserSearchMapper;
    @Override
    public void saveUserSearch(Map<String, Object> msgMap) {

        //取出参数
        Integer userId = (Integer)msgMap.get("userId");
        Integer equipmentId = (Integer)msgMap.get("equipmentId");
        String keyword = (String)msgMap.get("keyword");
        //查询行为实体
        ApBehaviorEntry behaviorEntry = apBehaviorEntryFeign.findByUserIdOrEquimentId(userId, equipmentId);
        if(behaviorEntry==null){return;}
        //查询记录是否存在
        QueryWrapper<ApUserSearch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("entry_id",behaviorEntry.getId());
        queryWrapper.eq("keyword",keyword);

        ApUserSearch userSearch = getOne(queryWrapper);
        if(userSearch==null) {
            //添加记录
            userSearch = new ApUserSearch();
            userSearch.setEntryId(behaviorEntry.getId());
            userSearch.setKeyword(keyword);
            userSearch.setStatus(true);
            userSearch.setCreatedTime(new Date());
            save(userSearch);
        }else {
            //修改记录
            userSearch.setStatus(true);
            userSearch.setCreatedTime(new Date());
            updateById(userSearch);
        }
    }

    @Override
    public Result<List<ApUserSearch>> load(UserSearchDto dto) {
        if(dto.getPageNum()==0)dto.setPageNum(1);
        if(dto.getPageSize()==0)dto.setPageSize(5);

        Integer userId = null;
        ApUser apUser = (ApUser) ThreadLocalUtils.get();
        if(apUser!=null) {
            userId = apUser.getId();
        }
        //查询行为实体
        ApBehaviorEntry behaviorEntry = apBehaviorEntryFeign.findByUserIdOrEquimentId(userId, dto.getEquipmentId());
        if(behaviorEntry==null)return Result.ok();

        try {
            IPage<ApUserSearch> iPage = new Page<>(dto.getPageNum(), dto.getPageSize());
            QueryWrapper<ApUserSearch> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq("entry_id",behaviorEntry.getId());
            queryWrapper.eq("status",1);
            queryWrapper.orderByDesc("created_time");

            iPage = apUserSearchMapper.selectPage(iPage, queryWrapper);

            return Result.ok(iPage.getRecords());
        } catch (Exception e) {
            e.printStackTrace();
            throw new LeadNewsException(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public Result delUserSearch(UserSearchDto dto) {
        try {
            ApUserSearch apUserSearch = new ApUserSearch();
            apUserSearch.setId(dto.getId());
            apUserSearch.setStatus(false);
            apUserSearch.setCreatedTime(new Date());
            apUserSearchMapper.updateById(apUserSearch);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new LeadNewsException(AppHttpCodeEnum.SERVER_ERROR);
        }

    }
}
